package com.sms.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sms.common.A;
import com.sms.pojo.Products;
import com.sms.service.impl.ProductsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
//商品列表
@WebServlet("/ProductsList")
public class ProductsListController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//        String _pageIndex=request.getParameter("pageIndex");
//        String _pageSize=request.getParameter("pageSize");
//        Integer pageIndex= StrUtil.isNotEmpty(_pageIndex)?Integer.parseInt(_pageIndex):1;
//        Integer pageSize=StrUtil.isNotEmpty(_pageSize)?Integer.parseInt(_pageSize):5;
//
//        String orderBy=request.getParameter("orderBy");
//
//        ProductsServiceImpl service=new ProductsServiceImpl();
//        if(StrUtil.isNotEmpty(orderBy)){
//            PageHelper.startPage(pageIndex,pageSize,orderBy);
//        }else{
//            PageHelper.startPage(pageIndex,pageSize);
//        }
//
//        List<Products> list = service.listProducts();
//        PageInfo<Products> pageInfo=new PageInfo<>(list);
//        service.getSqlSession().close();
//        response.getWriter().write(JSONUtil.toJsonStr(A.success("商品信息查询成功", pageInfo), JSONConfig.create().setDateFormat("yyyy-MM-dd")));


        ProductsServiceImpl service=new ProductsServiceImpl();

        List<Products> list = service.listProducts();

        service.getSqlSession().close();

        response.getWriter().write(JSONUtil.toJsonStr(A.success("商品信息查询成功", list), JSONConfig.create().setDateFormat("yyyy-MM-dd")));

    }
}
